package com.jiyun.order.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.jiyun.order.mapper.OrderMapper;
import com.jiyun.order.service.OrderService;
import com.jiyun.pojo.*;
import com.jiyun.pojo.driver.CarAttribute;
import com.jiyun.pojo.driver.DriverAttribute;
import com.jiyun.pojo.order.*;
import com.jiyun.pojo.order.vo.DriverVo;
import com.jiyun.pojo.order.vo.OrderVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Logger;

@Slf4j
@Service
public class OrderServiceImpl implements OrderService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private ThreadPoolTaskExecutor goodsThreadPool;

    @Override
    public List<OrderVo> list(String userId) {
        long start = System.currentTimeMillis();

        List<OrderVo> list = new ArrayList<>();
        //查找订单基础信息
        List<OrderInformation> orderInformations = orderMapper.getOrderInformation(userId);
        for (OrderInformation information : orderInformations) {
            //创建vo对象
            OrderVo vo = new OrderVo();
            //根据订单ID查找订单费用
            CompletableFuture<OrderCost> ordercost = CompletableFuture.supplyAsync(() ->
                    orderMapper.getOrderCost(information.getOrderId()), this.goodsThreadPool
            );
            //根据订单ID查找订单时间
            CompletableFuture<OrderTime> orderTime = CompletableFuture.supplyAsync(() ->
                    orderMapper.getOrderTime(information.getOrderId()), this.goodsThreadPool
            );
            //等待上面执行结束
            CompletableFuture.allOf(ordercost, orderTime).join();
            //赋值基本信息
            vo.setOrderInformation(information);
            log.error("订单信息：" + information);
            //赋值订单费用
            vo.setOrderCost(ordercost.join());
            log.error("订单费用信息：" + ordercost.join());
            //赋值订单时间
            vo.setOrderTime(orderTime.join());
            log.error("订单时间信息：" + orderTime.join());
            //添加到集合中
            list.add(vo);
        }
        System.out.println("耗时：" + (System.currentTimeMillis() - start));
        //根据订单时间倒序
        list.sort((o1, o2) -> o2.getOrderTime().getOrderTime().compareTo(o1.getOrderTime().getOrderTime()));
        return list;
    }


    @Override
    public OrderVo findOrderById(String orderId) {
        OrderVo vo = new OrderVo();
        //查找订单基础信息
        CompletableFuture<OrderInformation> information = CompletableFuture.supplyAsync(() ->
                orderMapper.getOrderById(orderId), this.goodsThreadPool
        );
        //根据订单ID查找订单费用
        CompletableFuture<OrderCost> ordercost = CompletableFuture.supplyAsync(() ->
                orderMapper.getOrderCost(orderId), this.goodsThreadPool
        );
        //根据订单ID查找订单时间
        CompletableFuture<OrderTime> orderTime = CompletableFuture.supplyAsync(() ->
                orderMapper.getOrderTime(orderId), this.goodsThreadPool
        );
        //等待上面执行结束
        CompletableFuture.allOf(information, ordercost, orderTime).join();
        //赋值基本信息
        vo.setOrderInformation(information.join());
        log.error("订单信息：" + information.join());
        //赋值订单费用
        vo.setOrderCost(ordercost.join());
        log.error("订单费用信息：" + ordercost.join());
        //赋值订单时间
        vo.setOrderTime(orderTime.join());
        log.error("订单时间信息：" + orderTime.join());
        return vo;
    }

    @Override
    public DriverVo findDriverById(Integer driverId) {
        //创建对象
        DriverVo driverVo = new DriverVo();
        //查找司机基本信息
        CompletableFuture<DriverAttribute> driverAttribute = CompletableFuture.supplyAsync(() ->
                orderMapper.getDriverById(driverId), this.goodsThreadPool
        );
        //查找司机车辆基本信息
        CompletableFuture<CarAttribute> carAttribute = CompletableFuture.supplyAsync(() ->
                orderMapper.getCarById(driverId), this.goodsThreadPool
        );

        //等待上面执行结束
        CompletableFuture.allOf(driverAttribute, carAttribute).join();
        driverVo.setDriverAttribute(driverAttribute.join());
        driverVo.setCarAttribute(carAttribute.join());
        if (ObjectUtil.isNotEmpty(driverVo.getCarAttribute())) {
            //查找司机的车辆详细信息
            BaseCarTypeManage baseCarTypeManage = orderMapper.getBaseCarTypeManageById(driverVo.getCarAttribute().getCarVehicleModelId());
            driverVo.setBaseCarTypeManage(baseCarTypeManage);
        }
        return driverVo;
    }

    @Override
    public void insertComplain(OrderComplaint orderComplaint) {
        orderComplaint.setComplaintTime(new Date());
        orderMapper.insertComplain(orderComplaint);
    }

    @Override
    public OrderComplaint getComplain(String orderId) {
        return orderMapper.getComplain(orderId);
    }

    @Override
    public void insertUserAppraise(OrderAppraise orderAppraise) {
        orderMapper.insertUserAppraise(orderAppraise);
    }

    @Override
    public OrderAppraise getUserAppraise(String orderId) {
        return orderMapper.getUserAppraise(orderId);
    }

    @Override
    public void UpdateUserAppraise(OrderAppraise orderAppraise) {
        orderMapper.UpdateUserAppraise(orderAppraise);
    }

    @Transactional      //使用事物
    @Override
    public void updateOrderStatus(String orderId) {
        //修改基本订单信息里面的订单状态
        orderMapper.updateOrderInformationOrderStatus(orderId);
        //修改订单费用表的订单状态
        orderMapper.updateOrderCostPayStatus(orderId);
        //修改订单时间表里面的支付时间
        OrderTime orderTime = new OrderTime();
        orderTime.setOrderId(orderId);
        orderTime.setPayTime(new Date());
        orderMapper.updateOrderTime(orderTime);
    }

}
